<?php
/**
 * AJAX Handler for Sites: List - List sites (of a certain type) for the project.
 * NOTE:    A Site has been standardised from Websites, Documentation Links, and Blog Links.
 *	    This is because they are all in the same format. A Change here affects all 3.
 * 
 * I have made sure Libdebug is silent in AJAX. This is because it can intefere with the HTML/Javascript as Libdebug Appends after </html>.
 * If you have a problem and need to debug comment $_LIBDEBUG->silence(); (the framework must also be in debugging mode for this to work)
 */

    $_ECLIPSEMDE = array();
    
    // Disable Templating System
    $_ECLIPSEMDE['TEMPLATING'] = false;
    require_once('../../SiteIncludes.GLOBAL.php');
    
    // Tell libdebug to never print out error information - overrides debugging flags in fwork
    $_LIBDEBUG->silence();
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	header("HTTP/1.1 400 Bad Request");
	die('');
    }
    
    // Ensure all variables needed are set
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false && isset($gVAR['type']) !== false && isset($gVAR['alias']) !== false) {
	try {
	    $Project =& $ProjectStash->get($gVAR['alias']);
	    
	    if($Project->userCanEdit($_LOGGEDINUSER->get_userid()) === true) {
		
		$Project->instanciate();
		if($gVAR['type'] == 'site') {
		   $vals = $Project->getWebsites();
		} elseif($gVAR['type'] == 'docs') {
		    $vals = $Project->getDoclinks();
		} elseif($gVAR['type'] == 'blog') {
		    $vals = $Project->getBlogs();
		} elseif($gVAR['type'] == 'rss') {
		    $vals = $Project->getRSSfeeds();
		} else {
		    header("HTTP/1.1 400 Bad Request");
		    die();
		}
		
		$js_name = '';
		$js_i = 0;
		foreach($vals as $key => $value) {
		    $js_name .= "x_name[{$js_i}] = '{$value}';\n";
		    $js_i++;
		}
		echo <<<EOHTML

		    <!DOCTYPE html>
		    <html>
			<head><title></title></head>
			<body>
			<script type="text/javascript">
			    var x_name = new Array();
			    {$js_name}
				parent.SiteEDJX.update('{$gVAR['type']}', x_name);
			</script>
			    
			</body>
		    </html>
		
EOHTML;
		
	    } else {
		throw new InvalidProjectException('');
	    }
	    
	} catch(InvalidProjectException $e) {
	    header("HTTP/1.1 400 Bad Request");
	    die('');
	}
    } else {
	header("HTTP/1.1 400 Bad Request");
	die('');
    }
?>
